// pages/mybook/mybook.js

const app = getApp();
var funcs = require('../../static/js/func.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    category_list: ['代办','已完成','已逾期'],
    select_category: 0,
    instname: "广元农商行办事处测试",
    bookinfos: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this
    console.log("监听到预约记录页面显示")
    let openid = app.globalData.openid //获取用户openid
    let status = this.data.select_category
    console.log("选中的请求数据为：>"+openid+">"+status)
    funcs.getBookInfo(openid,status).then(res=>{
      if(res.data.succeed){
        console.log("bookinfos:>",res.data.data)
        that.setData({
          bookinfos:res.data.data
        })
      }
    })
  },
  junmToHome: function (e) {
    let datas = e.currentTarget.dataset;
    console.log(datas)
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        let latitude = res.latitude
        let longitude = res.longitude

        let shop_latitude = datas.latitude
        let shop_longitude = datas.longitude
        let name = datas.name
        let distance = funcs.getDistance2(latitude, longitude, shop_latitude, shop_longitude);
        datas = {
          name: name,
          distance:distance
        }
        var jsonString = JSON.stringify(datas);
        //跳转页面并传递json字符串
        wx.navigateTo({
          url: '/pages/home/home?params=' + encodeURIComponent(jsonString)
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  errorNotice:function(res){
    wx.showToast({
        title: "目前暂不开放该功能",
        icon: 'none',
        duration: 2000
      })
  },
  commentTodo:function(res){
    let data = res.currentTarget.dataset
    let book_id = data.bookid;
    this.errorNotice()
  },
  reBookTodo:function(res){
    let data = res.currentTarget.dataset
    let book_id = data.bookid;
  },
  cancel_book:function(res){
    let data = res.currentTarget.dataset
    let book_id = data.bookid;
    wx.showModal({
      title: "提示",
      content: "需要取消当前预约吗?",
      cancelText: '关闭',
      complete: (res) => {
        if (res.cancel) {
          return
        }
        if (res.confirm) {
          this.cancel(book_id)
        }
      }
    })

  },
    /**
   * 取消预约
   */
  cancel: function (book_id) {
    let _this = this;
    // wx.showLoading({
    //   title: '加载中'
    // })
    console.log("删除的bookid为：",book_id)
    funcs.cancelBook(book_id).then(res=>{
      if(res.data.succeed){
        wx.showModal({
          content: "取消成功",
          showCancel: false,
          success(res) {
            if (res.confirm) {
              let openid = app.globalData.openid //获取用户openid
              funcs.getBookInfo(openid,_this.data.select_category).then(res=>{
                if(res.data.succeed){
                  console.log("bookinfos:>",res.data.data)
                  _this.setData({
                    bookinfos:res.data.data
                  })
                }
              })
            }
          }
        })
        console.log("取消预约成功")
      }else {
        wx.showModal({
          content: res.data.errorMsg,
          showCancel: false,
        })
      }
    })
  },
  jumptomap: function (res) {
    // console.log("here")
    // console.log(res.currentTarget.dataset)
    let data = res.currentTarget.dataset

    let longitude = data.longitude;
    let latitude = data.latitude;
    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    let x = longitude - 0.0065;
    let y = latitude - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    let wz = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    let lon = z * Math.cos(wz);
    let lat = z * Math.sin(wz);
    longitude = lon;
    latitude = lat;
    console.log(latitude + latitude + data.nam + data.address)
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      name: data.name,
      address: data.address
    })
  },
  call_phone: function (res) {
    let data = res.currentTarget.dataset
    wx.showModal({
      title: "提示",
      content: "拨打 " + data.telephone + " ?",
      complete: (res) => {
        if (res.cancel) {
          return
        }
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: data.telephone,
            success: function () {
              console.log("拨打电话成功！")
            },
            fail: function () {
              console.log("拨打电话失败！")
            }
          })
        }
      }
    })
  },
  select_category:function(e){
    let index = e.currentTarget.dataset.category
    console.log(index)
    let that = this
    this.setData({
      select_category: index
    })
    let openid = app.globalData.openid //获取用户openid
    funcs.getBookInfo(openid,index).then(res=>{
      if(res.data.succeed){
        console.log("bookinfos:>",res.data.data)
        that.setData({
          bookinfos:res.data.data
        })
      }
    })
    // console.log(index)
    // console.log(this.data.category_list[index])
  }
})